package cn.com.pajx.pajx_visitor_v3.ui.fragment;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;

import java.util.LinkedHashMap;

import butterknife.BindView;
import butterknife.OnClick;
import cn.com.pajx.pajx_visitor_v3.R;
import cn.com.pajx.pajx_visitor_v3.api.Api;
import cn.com.pajx.pajx_visitor_v3.base.BaseMvpFragment;
import cn.com.pajx.pajx_visitor_v3.mvp.presenter.LoadDataPresenterImpl;
import cn.com.pajx.pajx_visitor_v3.utils.LogUtils;

/**
 * 修改管理密码页面
 * create an instance of this fragment.
 */
@SuppressLint("NonConstantResourceId")
public class ModifyPasswordFragment extends BaseMvpFragment<LoadDataPresenterImpl> {

    @BindView(R.id.view_old_password)
    EditText viewOldPassword;
    @BindView(R.id.view_new_password)
    EditText viewNewPassword;
    @BindView(R.id.view_confirm_password)
    EditText viewConfirmPassword;

    private String old_password, new_password, confirm_password;

    public ModifyPasswordFragment() {
    }

    public static ModifyPasswordFragment newInstance() {
        ModifyPasswordFragment fragment = new ModifyPasswordFragment();
        Bundle bundle = new Bundle();
        fragment.setArguments(bundle);
        return fragment;
    }

    @Override
    protected void beforeInitHead() {
        super.beforeInitHead();
    }

    @Override
    protected double loadWidthScale() {
        return 0.35;
    }

    @Override
    protected LoadDataPresenterImpl initPresenter() {
        return new LoadDataPresenterImpl();
    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_modify_password;
    }

    @Override
    protected void initView(View view, Bundle savedInstanceState) {
        initTextChangedListener();
    }

    /**
     * 事件监听
     */
    private void initTextChangedListener() {
        //旧密码
        viewOldPassword.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                old_password = s.toString().trim();
            }
        });
        //新密码
        viewNewPassword.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                new_password = s.toString().trim();
            }
        });
        //确认新密码
        viewConfirmPassword.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                confirm_password = s.toString().trim();
            }
        });
    }

    @OnClick({R.id.iv_close, R.id.tv_modify})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.iv_close://关闭
                dismiss();
                break;
            case R.id.tv_modify://修改
                LogUtils.e("old_password=" + old_password);
                LogUtils.e("new_password=" + new_password);
                LogUtils.e("confirm_password=" + confirm_password);
                modifyQueryPassword();
                break;
        }
    }

    private void modifyQueryPassword() {
        LinkedHashMap<String, String> paramsMap = new LinkedHashMap<>();
        paramsMap.put("old_password", old_password);
        paramsMap.put("pw_query", new_password);
        paramsMap.put("new_password", confirm_password);
        mPresenter.loadData(Api.MODIFY_PASSWORD, paramsMap, "MODIFY_PASSWORD", "正在修改");

    }

    @Override
    public void onSuccess(String data, String aesKey, String message, int code, String tag) {
        super.onSuccess(data, aesKey, message, code, tag);
        speckChinese("查询密码修改成功");
        dismiss();
    }

    @Override
    public void onError(String message, int code, String tag) {
        super.onError(message, code, tag);
        speckChinese(message);
    }
}